package com.guchenbo.example.thread;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.concurrent.CountDownLatch;

/**
 * @Author guchenbo
 * @Date 2019/7/3.
 */
public class MultiThreadTest implements Runnable {

    private CountDownLatch start = new CountDownLatch(1);
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private Object lock = new Object();
    private ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>(){
        @Override
        protected SimpleDateFormat initialValue() {
            SimpleDateFormat f =  new SimpleDateFormat("yyyy-MM-dd");
            System.out.println("init format " + f);
            return f;
        }
    };

    public static void main(String[] args) throws InterruptedException {
        MultiThreadTest test = new MultiThreadTest();
        test.threadLocal.set(test.format);
        for (int i = 0; i < 101; i++) {
            Thread t = new Thread(test);
            t.start();
        }
        test.start.countDown();
    }

    @Override
    public void run() {
        try {
            start.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName());
        //        format.format(new Date());
        try {
//            System.out.println(threadLocal.get());
//            threadLocal.get().parse("2016-03-21 12:00:00");
            threadLocal.get().parse("2016-03-21 12:00:00");
            //            synchronized (lock) {
            //
            //            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}